layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    var activityId = $("#activityId").val();
    //列表
    var tableIns = table.render({
        elem: '#entityList',
        url : '/winNum/jsonList',
        where: {activityId: activityId},
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 20,
        method:'post',
        limits : [10,15,20,25],
        id : "entityList",
        cols : [[
            {type: 'numbers', title: '序号'},
            {field: 'num',minWidth:120, title: '中奖号',align:'center'},
            {field: 'nickName',minWidth:120, title: '昵称',align:'center'},
            {field: 'phone',minWidth:120, title: '手机号',align:'center'},
            {field: 'createTime',minWidth:170, title: '创建时间', align:'center', templet:function(d){
                return d.createTime;
            }},
            {title: '操作',width:170,align:"center", templet:function(d){
                return '<a class="layui-btn layui-btn-xs" lay-event="edit">设置</a>';
            }}
        ]]
    });

    //搜索
    $(".search_btn").on("click",function(){
        table.reload("entityList",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                activityId: $("#activityId").val(),
                nickName: $("#nickName").val()
            }
        })
    });

    //增加/编辑
    function addEntity(params){
        var title="编辑";
        var activityId = $("#activityId").val();
        if(params==null||params==""){
            title="添加"
            params = "?catId="+catId;
        }else {
            params = params + "&catId="+catId;
        }
        var index = layui.layer.open({
            title :title,
            type : 2,
            content : "/activity/selectEntity"+params,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".closeLay").bind("click",function () {
                    layui.layer.close(index);
                })
                layui.layer.full(index);
            }
        });
    }
    $(".add_btn").click(function(){
        addEntity('');
    })

    //列表操作
    table.on('tool(entityList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            var activityId = $("#activityId").val();
            layer.confirm('是否设置【'+data.num+'】为默认中奖号？',{icon:3, title:'提示信息'},function(index){
                $.get("/winNum/defaultWinningNum",{
                    "activityId":activityId,
                    "winningNum":data.num
                },function(result){
                    if(1==result.returnInfo.state){
                        showLayerAlertNoRefre("设置成功！");
                    }else{
                        showLayerAlertNoRefre(result.returnInfo.errorMsg);
                    }
                })
            });
        } else if(layEvent === 'del'){ //删除
            layer.confirm('是否删除商品活动【'+data.title+'】吗？',{icon:3, title:'提示信息'},function(index){
                $.get("/activity/deleteEntity",{
                    "id":data.id,
                    "_method":"DELETE"
                },function(result){
                    if(1==result.returnInfo.state){
                        tableIns.reload();
                        layer.close(index);
                    }else{
                        top.layer.msg(result.returnInfo.errorMsg);
                    }
                })
            });
        } else if(layEvent === 'list'){
            window.location.href = "/activity/selectEntity"+"?itemId="+data.id;
        }
    });

})